package edu.chc.hhsh.ext.ext_search;

import com.pub.database.*;
import javax.servlet.http.*;
import bsh.*;

public class Action {

    QueryBean gqbnDAO = null;

    public String action(javax.servlet.jsp.PageContext pageContext) {
        HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
        HttpServletResponse response = (HttpServletResponse) pageContext.getResponse();

        View view = (View) request.getAttribute("view");
//       (new WebFunction()).setPortal_to_session(request); //set session from portal
//       if (request.getSession().getAttribute("u_id")==null) {
//          try{
//             request.getRequestDispatcher((new StringBuffer("login.jsp")).insert(0,"/").toString()).forward(request,response);
//             return "login";
//          }
//          catch(Exception le){
//             view.setOnloadScript("can not redirect to login page...please contact IT");
//             return "";
//          }
//       }

        response.setHeader("Cache.Control", "no-cache");
        response.setHeader("Pragma", "no-cache");
        response.setDateHeader("Expires", 0);

//       view.setLoginUserNO(request.getSession().getAttribute("u_id").toString());
        view.setLoginUserNO("05227");

        String lstrResult = null;
        Business business = new Business();
        DataView DataView = null;
        DataViewForm DataViewForm = null;
        Interpreter lbhsIPR = new Interpreter();//可動態執行字串中的程式碼物件
        StringBuffer lstbDynamicCode = new StringBuffer();

        try {
            if (view.getPrgAction() != null) {
                /* 這段為每支Action.java 唯一會有差異的地方 */
                gqbnDAO = new QueryBean(view.getDBAlias(), view.getDBDebug(), view.getSourceCharset(), view.getTargetCharset());

                //只有在使用者進行操作(有PrgAction)產生時，Business才會用到QueryBean
                business.SetQueryBean(gqbnDAO);
                
                DataView = new DataView(gqbnDAO);
                DataViewForm = new DataViewForm();

                request.setAttribute("DataViewForm", DataViewForm);

                //Flow flow=new Flow(gqbnDAO,"AMS",view,business);

                view.setDataView(DataView);
                view.setDataViewForm(DataViewForm);
                //view.setFlow(flow);
                /***************************************/
                
                //每次submit一開始都要做的事
                lstrResult = business.actionForInit(view);

                if (view.getPrgAction().equals("getData")) {//利用AJAX即時讀取資料
                    //需先定義字串中的物件變數
                    lbhsIPR.set("business", business);
                    lbhsIPR.set("view", view);

                    lstrResult = business.getData(view);

                    pageContext.getOut().clear();
                    pageContext.getOut().write(lstrResult);
                    pageContext.getOut().flush();
                    return "login";
                } else {
                    //執行各動作對應的method
                    lstbDynamicCode.append("business.actionFor");
                    lstbDynamicCode.append(view.getPrgAction().substring(0, 1).toUpperCase());
                    lstbDynamicCode.append(view.getPrgAction().substring(1));
                    lstbDynamicCode.append("(view);");
                    //執行字串中的動態程式碼
                    lstrResult = lbhsIPR.eval(lstbDynamicCode.toString()).toString();
                }

                lstrResult = business.actionForEnd(view);
            }

            business.setButtonShow(view, view.getPrgAction(), view.getPrgStatus());
            business.setReadonlyFields(view, view.getPrgAction(), view.getPrgStatus());
            business.setShowFields(view, view.getPrgAction(), view.getPrgStatus());

            return "";
        } catch (Exception e) {
            view.setOnloadScript("alert('" + e.toString().replaceAll("'", "′") + ", please contact IT.');");
            e.printStackTrace();
            return "";
        } finally {
            try {
                if (gqbnDAO != null) {
                    gqbnDAO.close();
                }
            } catch (Exception e) {
                System.out.println("can not close querybean in Action.java");
            }
        }
    }
}
